package com.hxht.cmp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hxht.cmp.entity.*;
import com.hxht.cmp.mapper.*;
import com.hxht.cmp.pojo.MajorCasePojo;
import com.hxht.cmp.service.MajorService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author alice on 2017/9/7 0007.
 * @version 1.0
 * @since 1.0
 */
@Service
public class MajorServiceImp implements MajorService {
    @Resource
    private MajorApplyMapper majorApplyMapper;
    @Resource
    private MajorCaseMapper majorCaseMapper;
    @Resource
    private MajorPlanMapper majorPlanMapper;
    @Resource
    private MajorTrialMapper majorTrialMapper;
    @Resource
    private MajorVideoMapper majorVideoMapper;
    @Resource
    private MajorEvidenceMapper majorEvidenceMapper;

    @Override
    public int insertMajorApply(MajorApply majorApply) {
        return majorApplyMapper.insert(majorApply);
    }

    @Override
    public MajorCase selectCaseByUid(String key) {
        MajorCase majorCase = new MajorCase();
        majorCase.setUid(key);
        return majorCaseMapper.selectOne(majorCase);
    }

    /**
     * 查询大案要案相关排期
     *
     * @param key
     * @return
     */
    @Override
    public List<MajorPlan> selectPlanByCaseUid(String key) {
        MajorPlan majorPlan = new MajorPlan();
        majorPlan.setCaseUid(key);
        return majorPlanMapper.select(majorPlan);
    }

    /**
     * 查询大案要案相关排期
     *
     * @param key
     * @return
     */
    @Override
    public MajorPlan selectPlanByUid(String key) {
        MajorPlan majorPlan = new MajorPlan();
        majorPlan.setUid(key);
        return majorPlanMapper.selectOne(majorPlan);
    }

    /**
     * 查询大案要案相关开庭记录
     *
     * @param uids
     * @return
     */
    @Override
    public List<MajorTrial> selectTrialInPlanUid(List<String> uids) {
        if (ObjectUtils.isEmpty(uids)) {
            return null;
        }
        Example example = new Example(MajorTrial.class);
        example.createCriteria().andIn("planUid", uids);
        return majorTrialMapper.selectByExample(example);
    }

    /**
     * 查询大案要案相关开庭记录
     *
     * @param key
     * @return
     */
    @Override
    public MajorTrial selectTrialByUid(String key) {
        MajorTrial trial = new MajorTrial();
        trial.setUid(key);
        return majorTrialMapper.selectOne(trial);
    }

    /**
     * 查询大案要案相关视频
     *
     * @param uids
     * @return
     */
    @Override
    public List<MajorVideo> selectVideoInTrialUid(List<String> uids) {
        if (ObjectUtils.isEmpty(uids)) {
            return null;
        }
        Example example = new Example(MajorVideo.class);
        example.createCriteria().andIn("trialUid", uids);
        return majorVideoMapper.selectByExample(example);
    }

    /**
     * 根据开庭记录（trialUid）查询大案要案视频
     *
     * @param key
     * @return
     */
    @Override
    public List<MajorVideo> selectVideoByTrialUid(String key) {
        Example example = new Example(MajorVideo.class);
        example.createCriteria().andCondition("trial_uid = ", key);
        example.setOrderByClause("chn asc");
        return majorVideoMapper.selectByExample(example);
    }

    /**
     * 查询大案要案相关资料
     *
     * @param key
     * @return
     */
    @Override
    public List<MajorEvidence> selectEvidenceByCaseUid(String key, String userUid) {
        if (Objects.equals("super", userUid)) {
            MajorEvidence majorEvidence = new MajorEvidence();
            majorEvidence.setMajorCaseUid(key);
            majorEvidence.setActive(1);
            return majorEvidenceMapper.select(majorEvidence);
        }
        MajorApply majorApply = new MajorApply();
        majorApply.setCaseUid(key);
        majorApply.setUserUid(userUid);
        List<MajorApply> majorApplies = majorApplyMapper.select(majorApply);
        if (!ObjectUtils.isEmpty(majorApplies)) {
            boolean success = false;
            for (MajorApply apply : majorApplies) {
                if (apply.getExpiryDate().getTime() > new Date().getTime()) {
                    success = true;
                }
            }
            if (success) {
                MajorEvidence majorEvidence = new MajorEvidence();
                majorEvidence.setMajorCaseUid(key);
                majorEvidence.setActive(1);
                return majorEvidenceMapper.select(majorEvidence);
            }
        }
        return null;
    }

    public Page<MajorCase> selectMajorCase(MajorCasePojo majorCase) {
        Example example = new Example(MajorCase.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andCondition("active = ", "1");
        if (!StringUtil.isEmpty(majorCase.getCourtUid())) {
            criteria.andCondition("court_uid = ", majorCase.getCourtUid());
        }
        if (!StringUtil.isEmpty(majorCase.getCategory())) {
            criteria.andCondition("category = ", majorCase.getCategory());
        }
        if (!StringUtil.isEmpty(majorCase.getArchives())) {
            criteria.andLike("archives", majorCase.getArchives());
        }
        if (!StringUtil.isEmpty(majorCase.getName())) {
            criteria.andLike("name", majorCase.getName());
        }
        if (!StringUtil.isEmpty(majorCase.getAccuse())) {
            criteria.andLike("accuse", majorCase.getAccuse());
        }
        example.setOrderByClause("filing_date desc");
        return PageHelper.startPage(majorCase.getPage(), majorCase.getPageSize()).doSelectPage(() -> majorCaseMapper.selectByExample(example));
    }

    /**
     * 查询大案要案相关资料
     *
     * @param key
     * @return
     */
    @Override
    public MajorEvidence selectEvidenceByPrimaryKey(long key) {
        return majorEvidenceMapper.selectByPrimaryKey(key);
    }
}
